package com.fx.easyexcel.demo.controller;

import com.fx.easyexcel.demo.service.FileService;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.CRC32;
import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * @author Faxon
 * @version 1.0
 * @date 2020-07-05 13:45
 **/
@Controller
public class PageController {

    @Resource
    FileService fileService;

    @RequestMapping("/")
    public String index() {
        return "index";
    }

    private static final int DEFAULT_BUFF_SIZE = 1024 * 4;

    @PostMapping("/test")
    public void downloadTest(HttpServletResponse response) {
        String buket = "http://s3-us-west-1.amazonaws.com/com.learning-genie.archives.pdf/pdf/";
        String path1 = buket + "70dbaa20-066a-40f2-8725-8242a87ab72e.pdf";
        String path2 = buket + "5dd5b995-2091-426c-af69-d41d5efa067d.pdf";
        String path3 = buket + "a8f66a1a-8276-426a-8962-b15a63f431ed.pdf";
        String[] paths = new String[]{path1, path2, path3};
        List<File> fileList = new ArrayList<>();
        try {
            //下载文件（这里可能有问题）
            for (String path : paths) {
                URL url = new URL(path);
                InputStream is = url.openStream();
                String fileName = path.replace(buket, "");
                File file = new File(fileName);
                FileOutputStream fos = new FileOutputStream(file);
                IOUtils.copy(is, fos);
                fos.close();
                is.close();
                fileList.add(file);
            }

            //压缩文件
            File zip = new File("1.zip");
            CheckedOutputStream cos = new CheckedOutputStream(new FileOutputStream(zip), new CRC32());
            ZipOutputStream zos = new ZipOutputStream(cos);
            for (File file : fileList) {
                ZipEntry entry = new ZipEntry(file.getName());
                zos.putNextEntry(entry);
                BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
                int count;
                byte[] data = new byte[DEFAULT_BUFF_SIZE];
                while ((count = bis.read(data, 0, DEFAULT_BUFF_SIZE)) != -1) {
                    zos.write(data, 0, count);
                }
                bis.close();
            }
            zos.closeEntry();

            FileInputStream inputData = new FileInputStream(zip);
            //放到缓冲流里面
            BufferedInputStream bins = new BufferedInputStream(inputData);
            //获取文件输出IO流
            OutputStream outs = response.getOutputStream();
            BufferedOutputStream bouts = new BufferedOutputStream(outs);
            response.setContentType("application/x-download");
            response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode("1.zip", "UTF-8"));
            int bytesRead = 0;
            byte[] buffer = new byte[8192];
            //开始向网络传输文件流
            while ((bytesRead = bins.read(buffer, 0, 8192)) != -1) {
                bouts.write(buffer, 0, bytesRead);
            }
            bouts.flush();//这里一定要调用flush()方法
            inputData.close();
            bins.close();
            outs.close();
            bouts.close();

            //删除临时文件 ... 代码需优化
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
